
import sys, os, re
import urllib
from lxml import etree
from dateutil import parser as dateparser
from baseHandler import *

baseuri = "http://www.webarchive.org.uk/wayback/archive/*/"

class BLHandler(BaseProxyHandler):

    def fetch_changes(self, req, requri, dt=None):
        # implement the changes list for this particular proxy

        uri = baseuri + requri
        try:
            fh = urllib.urlopen(uri)
        except:
            return self.error("Couldn't retrieve data from %s" % uri, req, status=404)
        data = fh.read()
        fh.close()

        try:
            dom = etree.XML(data)
        except:
            return self.error("Couldn't parse data from %s" % uri, req, status=500)

        if not dom:
            return

        changes = []

        rlist = dom.xpath('/wayback/results/result')
        for a in rlist:

            dtstr = a.xpath('./capturedate/text()')[0]
            url = a.xpath('./url/text()')[0]
            loc = "http://www.webarchive.org.uk/wayback/archive/%s/%s" % (dtstr, url)

            dtstr += " GMT"
            dtobj = dateparser.parse(dtstr)
            changes.append((dtobj, loc, {'last': dtobj, 'obs' : 1}))

        return changes


def handler(req):
    hdlr = BLHandler('bl')
    return basehandler(req, hdlr)


